package com.kinyx.framework.data;

import java.util.ArrayList;
import java.util.HashMap;

import com.kinyx.framework.annotation.assist.CanNotNull;
import com.kinyx.framework.annotation.assist.CanNull;
import com.kinyx.framework.annotation.assist.ReturnNotNull;
import com.kinyx.framework.annotation.assist.ReturnNull;
import com.kinyx.framework.annotation.assist.Tester;
import com.kinyx.framework.annotation.assist.Throws;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * XML工具类
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class KXmlUtils {

	/**
	 * 判断字符串是否为XML字符串
	 * @param xml XML字符串
	 * @return true：是XML字符串；false：不是XML字符串；
	 */
	@ReturnNotNull
	public static boolean isXml(@CanNull final String xml) {
		return KXmlUtils4Jackson.isXml(xml);
	}

	/**
	 * XML字符串转JSON字符串
	 * @param xml XML字符串
	 * @param nullable true: 允许null值; false: 不允许null值;
	 * @param isArray true: 数组; false: 对象;
	 * @return JSON字符串
	 */
	@ReturnNull("json == null && nullable == true")
	@Throws(at = "KObjectMapper.toHashMap(ObjectMapper, String)", value = "JsonMappingException", when = "ObjectMapper.readValue(String, JavaType)")
	@Throws(at = "KObjectMapper.toHashMap(ObjectMapper, String)", value = "JsonProcessingException", when = "ObjectMapper.readValue(String, JavaType)")
	@Throws(at = "KObjectMapper.writeValueAsString(ObjectMapper, Object)", value = "JsonProcessingException", when = "ObjectMapper.writeValueAsString(Object)")
	public static String toJson(@CanNull final String xml, @CanNotNull final boolean nullable, @CanNotNull final boolean isArray) {
		return KXmlUtils4Jackson.toJson(xml, nullable, isArray);
	}

	/**
	 * 对象转XML字符串
	 * @param obj 待转换对象
	 * @param nullable true: 允许null值; false: 不允许null值;
	 * @param isArray true: 数组; false: 对象;
	 * @return XML字符串
	 */
	@ReturnNull("obj == null && nullable == true")
	@Throws(at = "KObjectMapper.writeValueAsString(ObjectMapper, Object)", value = "JsonProcessingException", when = "ObjectMapper.writeValueAsString(Object)")
	@Tester("com.kinyx.framework.data.KXmlUtils_Tester_toXml")
	public static String toXml(@CanNull final Object obj, @CanNotNull final boolean nullable, @CanNotNull final boolean isArray) {
		return KXmlUtils4Jackson.toXml(obj, nullable, isArray);
	}

	/**
	 * XML字符串转对象
	 * @param <T> 泛型
	 * @param xml XML字符串
	 * @param clazz 对象类型
	 * @return 对象
	 */
	@ReturnNull("KStringUtils.isBlank(xml)")
	@Throws(at = "KObjectMapper.toBean(ObjectMapper, String, Class)", value = "JsonMappingException", when = "ObjectMapper.readValue(String, Class)")
	@Throws(at = "KObjectMapper.toBean(ObjectMapper, String, Class)", value = "JsonProcessingException", when = "ObjectMapper.readValue(String, Class)")
	public static <T> T toBean(@CanNull final String xml, @CanNotNull final Class<T> clazz) {
		return KXmlUtils4Jackson.toBean(xml, clazz);
	}

	/**
	 * XML字符串转HashMap对象
	 * @param xml XML字符串
	 * @return HashMap对象
	 */
	@ReturnNull("KStringUtils.isBlank(xml)")
	@Throws(at = "KObjectMapper.toHashMap(ObjectMapper, String)", value = "JsonMappingException", when = "ObjectMapper.readValue(String, JavaType)")
	@Throws(at = "KObjectMapper.toHashMap(ObjectMapper, String)", value = "JsonProcessingException", when = "ObjectMapper.readValue(String, JavaType)")
	public static HashMap<String, String> toHashMap(@CanNull final String xml) {
		return KXmlUtils4Jackson.toHashMap(xml);
	}

	/**
	 * XML字符串转ArrayList对象
	 * @param <T> 泛型
	 * @param xml XML字符串
	 * @param clazz 类
	 * @return ArrayList对象
	 */
	@ReturnNull("KStringUtils.isBlank(xml)")
	@Throws(at = "KObjectMapper.toArrayList(ObjectMapper, String, Class)", value = "JsonMappingException", when = "ObjectMapper.readValue(String, JavaType)")
	@Throws(at = "KObjectMapper.toArrayList(ObjectMapper, String, Class)", value = "JsonProcessingException", when = "ObjectMapper.readValue(String, JavaType)")
	public static <T> ArrayList<T> toArrayList(@CanNull final String xml, @CanNotNull final Class<T> clazz) {
		return KXmlUtils4Jackson.toArrayList(xml, clazz);
	}

}
